using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Logging;
using ProjetoComperio.Framework.Web.Security;
using ProjetoComperio.SystemFramework.Extensions;
using ProjetoComperio.SystemFramework.Web;
using User = ProjetoComperio.DataLayer.Entities.User;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    [ExceptionLogging]
    public class AcademicCoordinatorController : ComperioController
    {
        #region BLLsObjects
        private AcademicCoordinatorBLL _AcademicCoordinatorBLL;
        private AcademicCoordinatorBLL AcademicCoordinatorBLL
        {
            get
            {
                if (_AcademicCoordinatorBLL == null)
                    _AcademicCoordinatorBLL = new AcademicCoordinatorBLL();
                return _AcademicCoordinatorBLL;
            }
            set { _AcademicCoordinatorBLL = value; }

        }
        private StudentBLL _StudentBLL;
        private StudentBLL StudentBLL
        {
            get
            {
                if (_StudentBLL == null)
                    _StudentBLL = new StudentBLL();
                return _StudentBLL;


            }
            set { _StudentBLL = value; }
        }
        #endregion

        #region Create
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            var academicOrganizations  = new AcademicOrganizationBLL().RetrieveAcademicOrganizations().ToSelectedItemList("AcademicOrganizationID", "Name");
            academicOrganizations.Insert(0, new SelectListItem() { Selected = true });
            Session["AcademicOrganizations"] = academicOrganizations;
            
            var educationLevels =  new PersonBLL().RetrieveEducationLevels().ToSelectedItemList("EducationLevelID", "Description");
            educationLevels.Insert(0, new SelectListItem() { Selected = true });
            Session["EducationLevels"] = educationLevels;

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "PersonID")]AcademicCoordinator academicCoordinator)
        {
            var _user = new User();
            _user.UserName = Request["UserName"];
            _user.Password = Request["Password"];
            ViewData["UserFormData"] = _user;

            if (!ModelState.IsValid)
                return View();

            AcademicCoordinatorBLL.Create(academicCoordinator);

            // Creating user
            var user = new ProjetoComperio.Framework.Web.Security.User();
            user.UserName = Request["UserName"];
            user.Password = Request["Password"];
            user.Email = academicCoordinator.Email;
            
            ComperioMemberShipProvider comperioMemberShipProvider = new ComperioMemberShipProvider();
            user.Role = comperioMemberShipProvider.RetrieveRole(ComperioMemberShipProvider.Roles.Student);

            user.PersonID = academicCoordinator.PersonID;
            comperioMemberShipProvider.CreateUser(user,false);
            TempData["AcademicCoordinatorID"] = academicCoordinator.PersonID;

            return RedirectToAction("FinishEnrollment");
        }

        #endregion

        #region FinishEnrollment
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult FinishEnrollment()
        {
            var academicCoordinator = new AcademicCoordinatorBLL().RetrieveAcademicPersonByID(Convert.ToInt32(TempData["AcademicCoordinatorID"]));

            ViewData["AcademicCoordinatorName"] = academicCoordinator.Name;
            ViewData["AcademicCoordinatorID"] = academicCoordinator.PersonID;
            return View();

        }

        public override void FillEntity(Object entity, FormCollection form)
        {
            base.FillEntity(entity, form);
        }

        #endregion

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Edit()
        {
            //TrackedEntity<DataLayer.AcademicCoordinator> trackedEntity = TrackedEntity<AcademicCoordinator>.TrackedApplicationFactory();

            //trackedEntity.Entity.
            return View();
        }



        #region Ctor
        public AcademicCoordinatorController()
        {
            StudentBLL = new StudentBLL();
            AcademicCoordinatorBLL = new AcademicCoordinatorBLL();
        }

        #endregion
    }

}
